package com.shwread.android.qysw.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.customview.LoadMoreListView;
import com.shwread.android.ui.customview.LoadMoreListView.OnLoadMoreListener;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.http.face.QryEcardOrderRecorderListAction;
import logic.bean.BookTicketRecordInfo;
import logic.bean.TicketsColumninfos;
import logic.constants.DefaultConsts;
import logic.util.NetworkUtil;
import logic.util.Util;


/**
 * @Project： Shwread
 * @Title：BookTicketRecordActivity
 * @Description：电子券激活记录
 * @Author：wayne
 * @Date：2014年11月18日上午10:19:02
 */
public class BookTicketRecordActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener,
        OnItemClickListener
{
	private Context                 context;
	private Button                  btnBack, btnBookTicketRecharge;
	private TextView                tvTitle;
	private BookTicketRecordAdapter bookTicketRecoerAdapter;
	
	private LoadMoreListView        lvBookTicketRecord;
	private TicketsColumninfos      resultInfo;
	
	public int                      currentPage = 1;
	public int                      totalPage   = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.book_ticket_activate_record);
		this.context = this;
		initView();
		setListener();
		initData();
	}
	
	private Handler handler = new Handler()
	                        {
		                        @Override
		                        public void handleMessage(Message msg) {
			                        switch (msg.what) {
										case 100:
											lvBookTicketRecord.onLoadMoreComplete();
											if (resultInfo.getCardList().size() > 0)
											{
												bookTicketRecoerAdapter.notifyDataSetChanged();
											}
											break;
										default:
											break;
									}
								}
	                        };
	
	private void setListener() {
		btnBack.setOnClickListener(this);
		btnBookTicketRecharge.setOnClickListener(this);
		lvBookTicketRecord.setOnItemClickListener(this);
		
		lvBookTicketRecord.setOnLoadMoreListener(new OnLoadMoreListener()
		{
			
			@Override
			public void onLoadMore() {
				if (totalPage > currentPage)
				{
					currentPage++;
					new QryEcardOrderRecorderListAction(context, currentPage, 10, new BookTicketRecordListener2()).start();
				}
				else
				{
					lvBookTicketRecord.onLoadMoreComplete();
				}
			}
		});
	}
	
	@Override
	protected void onResume() {
		if (NetworkUtil.isNetAvailable(context))
		{
			new QryEcardOrderRecorderListAction(context, currentPage, 10, new BookTicketRecordListener1()).start();
		}
		else
		{
			new NetworkDialog(context).show();
		}
	    super.onResume();
	}
	
	private void initView() {
		btnBack = (Button) findViewById(R.id.book_ticket_activate_record_title_back_btn);
		tvTitle = (TextView) findViewById(R.id.book_ticket_activate_record_title_name_tv);
		btnBookTicketRecharge = (Button) findViewById(R.id.book_ticket_activate_record_title_book_ticket_btn);
		tvTitle.setText("激活记录");
		
		lvBookTicketRecord = (LoadMoreListView) findViewById(R.id.book_ticket_activate_record_lv);
	}
	
	private void initData() {
		bookTicketRecoerAdapter = new BookTicketRecordAdapter();
		lvBookTicketRecord.setAdapter(bookTicketRecoerAdapter);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.book_ticket_activate_record_title_back_btn:
				onBackPressed();
				break;
			case R.id.book_ticket_activate_record_title_book_ticket_btn:
				Intent intent = new Intent(BookTicketRecordActivity.this, BookTicketActivity.class);
				intent.putExtra(DefaultConsts.from_page, DefaultConsts.TAG_TICKETRECORD);
				startActivity(intent);
				break;
			default:
				break;
		}
		
	}
	
	@Override
	public Handler getUIHandler() {
		return handler;
	}
	
	class BookTicketRecordAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return (resultInfo == null || resultInfo.getCardList() == null) ? 0 : resultInfo.getCardList().size();
		}
		
		@Override
		public Object getItem(int position) {
			return (resultInfo == null || resultInfo.getCardList() == null) ? null : resultInfo.getCardList().get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = LayoutInflater.from(BookTicketRecordActivity.this).inflate(R.layout.book_ticket_activate_record_item, null);
				holder.ivIcon = (ImageView) convertView.findViewById(R.id.book_ticket_activate_record_item_icon_iv);
				holder.tvType = (TextView) convertView.findViewById(R.id.book_ticket_activate_record_item_type_tv);
				holder.TvTime = (TextView) convertView.findViewById(R.id.book_ticket_activate_record_item_time_tv);
				holder.tvMounth = (TextView) convertView.findViewById(R.id.book_ticket_activate_record_item_month_tv);
				holder.tvStatus = (TextView) convertView.findViewById(R.id.book_ticket_activate_record_item_status_tv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			
			BookTicketRecordInfo info = (BookTicketRecordInfo) getItem(position);
			holder.ivIcon.setBackgroundResource(R.drawable.book_ticket_activate_record_item_icon_bg);
			holder.tvType.setText("书券激活");
			holder.TvTime.setText(Util.getTimeByDuration(info.getCreateTime()));
			holder.tvMounth.setText(getResources().getString(R.string.add_service_month, info.getServiceMonthNum()));
			holder.tvStatus.setText("激活成功");
			return convertView;
		}
	}
	
	class ViewHolder
	{
		ImageView ivIcon;
		TextView  tvType, tvStatus;
		TextView  tvMounth, TvTime;
	}
	
	/**
	 * 首页显示
	 * 
	 * @author Administrator
	 */
	
	class BookTicketRecordListener1
	        implements
	        ActionListener
	{
		
		@Override
		public void OK(Object obj) {
			resultInfo = (TicketsColumninfos) obj;
			totalPage = resultInfo.getTotalPage();
			handler.sendEmptyMessage(100);
		}
		
		@Override
		public void ERROR(int resultCode, final String message) {
			Message msg = Message.obtain();
			msg.what = 101;
			msg.obj = message;
			handler.sendMessage(msg);
		}
		
		@Override
		public void HTTPERROR(int statusCode) {
			
		}
		
	}
	
	/**
	 * 下拉刷新显示
	 * 
	 * @author Administrator
	 */
	
	class BookTicketRecordListener2
	        implements
	        ActionListener
	{
		
		@Override
		public void OK(Object obj) {
			resultInfo = (TicketsColumninfos) obj;
			resultInfo.getCardList().addAll(resultInfo.getCardList());
			handler.sendEmptyMessage(100);
		}
		
		@Override
		public void ERROR(int resultCode, final String message) {
			Message msg = Message.obtain();
			msg.what = 101;
			msg.obj = message;
			handler.sendMessage(msg);
		}
		
		@Override
		public void HTTPERROR(int statusCode) {
			
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		final BookTicketRecordInfo bean = (BookTicketRecordInfo) parent.getAdapter().getItem(position);
		Intent intent = new Intent(BookTicketRecordActivity.this, BookTicketDetailActivity.class);
		intent.putExtra("BookTicketRecordInfo", bean);
		startActivity(intent);
	}
	
}
